<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" placeholder="用户编号" />
      <el-select v-model="listQuery.status" class="filter-item" placeholder="提现状态" title="提现状态"><el-option v-for="(item, index) in statusDic" :key="index" :label="item" :value="index" /></el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="序号" prop="id" sortable />

      <el-table-column align="center" label="提现用户" prop="userId" sortable />

      <el-table-column align="center" label="提现渠道" prop="channel" sortable />

      <el-table-column align="center" label="用户名" prop="wxAccount" />

      <el-table-column align="center" label="广告图片" prop="wxCollPic">
        <template slot-scope="scope">
          <el-image :src="thumbnail(scope.row.wxCollPic)" :preview-src-list="toPreview(scope.row, scope.row.wxCollPic)" style="width: 80px; height: 40px" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="提现金额" prop="price" />

      <el-table-column align="center" label="状态" prop="status" sortable>
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="处理用户" prop="operator" />
      <el-table-column align="center" label="完成时间" prop="updateTime" />
      <el-table-column align="center" label="创建时间" prop="addTime" />
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 用户编辑对话框 -->
    <el-dialog :visible.sync="userDialogVisible" title="收款信息">
      <el-form ref="userDetail" :model="userDetail" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="userDetail.userId" :disabled="true" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userDetail.username" />
        </el-form-item>
        <el-form-item label="微信号" prop="wxAccount">
          <el-input v-model="userDetail.wxAccount" />
        </el-form-item>

        <el-form-item label="收款二维码" prop="wxCollPic">
          <template slot-scope="scope">
            <el-image :src="thumbnail(userDetail.wxCollPic)" />
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUserUpdate">确定已付款</el-button>
        <el-button @click="userDialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { distributionWithdrawList, distributionWithdrawUpdate } from '@/api/withdraw'
import Pagination from '@/components/Pagination'
import { thumbnail, toPreview } from '@/utils'

export default {
  name: 'DistributionWithdraw',
  components: { Pagination },
  data() {
    return {
      thumbnail,
      toPreview,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        userId: undefined,
        username: undefined,
        sort: 'update_time',
        order: 'desc'
      },
      downloadLoading: false,
      channelDic: ['微信收款码'],
      statusDic: ['申请中', '提现成功', '提现失败', '已拒绝'],
      userDialogVisible: false,
      userDetail: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList: function() {
      this.listLoading = true
      distributionWithdrawList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    cancel() {
      this.init()
    },
    handleUserUpdate() {
      this.userDetail.status = 1
      distributionWithdrawUpdate(this.userDetail)
        .then((response) => {
          this.userDialogVisible = false
          this.$notify.success({
            title: '成功',
            message: '更新用户成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDetail(row) {
      this.userDetail = row
      this.userDialogVisible = true
    }
  }
}
</script>
<style scoped>
  .input-width {
    width: 50%;
  }
  .info {
    margin-left: 15px;
  }
</style>
